/**
 * Created by yang xiao     2019/06/21
 * Modified by zhai yd      2019/06/28
 * */


//拼接样式主方法
var rowData;
function switchMgr(e,data) {
    $("#switchContentBox").remove();
    rowData = data;
    var main_style = "switchContent";
    if(data.title != "all" && data != null){
        main_style = "switchContent_single"
    }
    var switchContentBox = $("<div class='switchContentBox' id='switchContentBox'></div>").appendTo("body");
    switchContentBox.css("height",$(window).height());
    var switchContent = $("<div class='"+main_style+"' id='switchContent'><div class='switch_close_btn' id='switch_close_btn' onclick='closeSwitch()'></div><div class='clear'></div></div>").appendTo(switchContentBox);
    var liangduBox = $("<div class='switchBox'>" +
            "<div class='switchMark lianduMark_off' id='liangduMark'></div>"+//关闭状态为_off.开启状态为_on
            "<div class='switchBtn'>" +
        "<a href='javascript:void(0);' class='turnOffBtn' onclick='turnOnLightSwitch(this);'></a>" +
        // "<a href='javascript:void(0);' class='turnOffBtn' onclick='turnOffLightSwitch(this);'></a>"+
        "</div>"+
        "<div class='liangdupercent' id='liangdupercent'><a onclick='liangdutiaozheng(this,0)' class='percentBtn ' href='javascript:void(0);'>0</a><a class='percentBtn' onclick='liangdutiaozheng(this,25)' href='javascript:void(0);'>25</a><a onclick='liangdutiaozheng(this,50)' class='percentBtn' href='javascript:void(0);'>50</a><a onclick='liangdutiaozheng(this,75)' class='percentBtn' href='javascript:void(0);'>75</a><a onclick='liangdutiaozheng(this,100)' class='percentBtn indexLight' href='javascript:void(0);'>100</a></div>"+
        "</div>");
    var jingbaoBox = $("<div class='switchBox'>" +
        "<div class='switchMark jingbaoMark_off' id='jingbaoMark'></div>"+//关闭状态为_off.开启状态为_on
        "<div class='switchBtn'>" +
        "<a href='javascript:void(0);' class='turnOffBtn' onclick='turnOnBaojingSwitch(this);'></a>" +
        // "<a href='javascript:void(0);' class='turnOffBtn' onclick='turnOffBaojingSwitch(this);'></a>"+
        "</div>"+
        "</div>");
    var menjinBox = $("<div class='switchBox'>" +
        "<div class='switchMark menjinMark_off' id='menjinMark'></div>"+//关闭状态为_off.开启状态为_on
        "<div class='switchBtn'>" +
        "<a href='javascript:void(0);' class='turnOffBtn' onclick='turnOnMenjinSwitch(this)'></a>" +
        "</div>"+
        "</div>");
    if(data.title == 门禁电子锁状态){   //门禁
        menjinBox.appendTo(switchContent);
    }
    else if(data.title == 报警开关状态){  //警报
        jingbaoBox.appendTo(switchContent);
    }
    else{
        liangduBox.appendTo(switchContent);
        jingbaoBox.appendTo(switchContent);
        menjinBox.appendTo(switchContent);
    }


}
//关闭弹框
var rke_clicktag = 0;
var alarm_clicktag = 0;
var light_clicktag = 0;
function closeSwitch() {
    $("#switchContentBox").hide();
    $("#switchContentBox").remove();
}
//灯开关
function turnOnLightSwitch(e) {
    if(light_clicktag == 0){
        light_clicktag = 1;
        if($(e).hasClass("turnOnBtn")){
            $(e).attr("class","turnOffBtn");
            $(".liangdupercent").hide();
            $("#liangduMark").removeClass("lianduMark_on");
            $("#liangduMark").attr("class","lianduMark_off");
            doCcontrol(rowData.id,liangdu_switch,liangdu_off);
        }
        else{
            $(e).attr("class","turnOnBtn")
            $(".liangdupercent").show();
            $("#liangduMark").removeClass("lianduMark_off");
            $("#liangduMark").attr("class","lianduMark_on");
            $(".percentBtn").each(function(){
                $(this).removeClass("indexLight");
                if($(this).html() == 100){
                    $(this).addClass("indexLight");
                    doCcontrol(rowData.id,liangdu,100);
                }
            });
            doCcontrol(rowData.id,liangdu_switch,liangdu_on);
        }
        setTimeout(function(){light_clicktag = 0},5000);
    }
    else{
        alert("点击太过频繁，5秒后再点击！")
    }



}
// function turnOffLightSwitch(e) {
//
// }
//报警开关

function turnOnBaojingSwitch(e) {
    if(alarm_clicktag == 0){
        alarm_clicktag = 1;
        if($(e).hasClass("turnOnBtn")){
            $(e).attr("class","turnOffBtn");
            $("#jingbaoMark").removeClass("jingbaoMark_on");
            $("#jingbaoMark").addClass("jingbaoMark_off");
            doCcontrol(rowData.id,alarm_switch,alarm_off);
        }
        else{
            $(e).attr("class","turnOnBtn")
            $("#jingbaoMark").removeClass("jingbaoMark_off");
            $("#jingbaoMark").addClass("jingbaoMark_on");
            doCcontrol(rowData.id,alarm_switch,alarm_on);
        }
        setTimeout(function(){alarm_clicktag = 0},5000);
    }
    else{
        alert("点击太过频繁，5秒后再点击！")
    }

}
// function turnOffBaojingSwitch(e) {
//     $("#jingbaoMark").removeClass("jingbaoMark_on");
//     $("#jingbaoMark").addClass("jingbaoMark_off");
// }
//门禁开关
function turnOnMenjinSwitch(e) {
    if(rke_clicktag == 0){
        rke_clicktag = 1;
        if($(e).hasClass("turnOnBtn")){
            $(e).attr("class","turnOffBtn");
            $("#menjinMark").removeClass("menjinMark_on");
            $("#menjinMark").addClass("menjinMark_off");
            doCcontrol(rowData.id,rke_lock_switch,rke_off);
        }
        else{
            $(e).attr("class","turnOnBtn")
            $("#menjinMark").removeClass("menjinMark_off");
            $("#menjinMark").addClass("menjinMark_on");
            doCcontrol(rowData.id,rke_lock_switch,rke_on);
        }
        setTimeout(function(){rke_clicktag = 0},5000);
    }
    else{
        alert("点击太过频繁，5秒后再点击！")
    }


}


//亮度调整
var liangdu_clicktag = 0;
function liangdutiaozheng(e,liangdu_value) {
    if(liangdu_clicktag == 0){
        liangdu_clicktag = 1;
        $("#liangdupercent").find("a").removeClass("indexLight");
        $(e).addClass("indexLight");
        $("#liangduMark").attr("class","switchMark lianduMark_on liangdu"+liangdu_value);
        doCcontrol(rowData.id,liangdu,liangdu_value);
        setTimeout(function(){liangdu_clicktag = 0},5000);
    }
    else{
        alert("点击太过频繁，5秒后再点击！")
    }
}

function doCcontrol(deviceId,projectInfoId,value){
    $.ajax({
        url:basePath+"/deviceManager/controlDevice",
        //processData: false,
        //data: xmlDocument,
        data:{
            deviceId : deviceId,
            projectInfoId : projectInfoId,
            value : value
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
           alert("网络延迟或系统异常,请联系管理员");
        }});
}